package com.softeem.j2001.controller;

import com.softeem.j2001.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class RedisController {
    @Autowired
    private RedisUtil redisUtil;
    @RequestMapping("/test")
    public String test() {
        return "test";
    }

    @ResponseBody
    @RequestMapping("/setRedisName")
    public Map<String, Object> setRedisName(String name) {
        Map<String, Object> result = new HashMap<>();
        try {
            redisUtil.set("name", name);
            result.put("states", 1);
        } catch (Exception e) {
            result.put("states", 0);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/getRedisName")
    public Map<String, Object> getRedisName() {
        Map<String, Object> result = new HashMap<>();
        try {
            String name = (String) redisUtil.get("name");
            result.put("states", 1);
            result.put("data", name);
        } catch (Exception e) {
            result.put("states", 0);
            result.put("data", null);
        }
        return result;
    }

}
